﻿using System;
using System.Collections.Generic;
using System.Data;

namespace Songhay.DataAccess.Extensions
{
    /// <summary>
    /// Extensions of <see cref="IDataReader"/>
    /// </summary>
    public static class IDataReaderExtensions
    {
        /// <summary>
        /// Converts the <see cref="IDataReader"/> into row values.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <exception cref="System.ArgumentNullException">The expected reader is not here.</exception>
        /// <remarks>
        /// for more info, see “Consuming a DataReader with LINQ”
        /// [http://www.thinqlinq.com/default/consuming-a-datareader-with-linq]
        /// </remarks>
        public static IEnumerable<object[]> ToRowValues(this IDataReader source)
        {
            if (source == null) throw new ArgumentNullException("The expected data reader is not here.");

            while (source.Read())
            {
                var row = new object[source.FieldCount];
                source.GetValues(row);
                yield return row;
            }
        }
    }
}
